<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="详情号" prop="detailCode">
        <el-input v-model="queryParams.detailCode" placeholder="请输入详情号" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="订单ID" prop="orderId">
        <el-input v-model="queryParams.orderId" placeholder="请输入订单ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="商品ID" prop="goodsId">
        <el-input v-model="queryParams.goodsId" placeholder="请输入商品ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入商品名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in wdm_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['ruoyi:goodsDetail:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['ruoyi:goodsDetail:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['ruoyi:goodsDetail:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['ruoyi:goodsDetail:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- 商品订单详情列表 -->
    <el-table v-loading="loading" :data="goodsDetailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" width="60" align="center" prop="detailId" />
      <!-- <el-table-column label="详情号" align="center" prop="detailCode" /> -->
      <!-- <el-table-column label="订单ID" align="center" prop="orderId" /> -->
      <!-- <el-table-column label="商品ID" align="center" prop="goodsId" /> -->
      <!-- <el-table-column label="商品名（实时）" align="center" prop="goodsId">
        <template #default="scope">
          <div v-for="item in goodsList" :key="item.goodsId">
            <span v-if="item.goodsId === scope.row.goodsId">
              {{ item.goodsName }}
            </span>
          </div>
        </template>
      </el-table-column> -->

      <!-- <el-table-column label="商品图（实时）" align="center" prop="goodsId">
        <template #default="scope">
          <image-preview
            v-if="getGoodsImage(scope.row.goodsId)"
            :src="getGoodsImage(scope.row.goodsId)"
            :width="50"
            :height="50"
          />
          <span v-else>无图片</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="商品名称" align="center" prop="goodsName" /> -->
      <!-- <el-table-column
        label="商品主图"
        align="center"
        prop="mainImage"
        width="100"
      >
        <template #default="scope">
          <image-preview :src="scope.row.mainImage" :width="50" :height="50" />
        </template>
      </el-table-column> -->
      <el-table-column label="下单时单价" align="center" prop="unitPrice">
        <template #default="scope">{{ scope.row.unitPrice }}￥</template>
      </el-table-column>
      <el-table-column label="购买数量" align="center" prop="quantity" />
      <el-table-column label="子项金额" align="center" prop="subtotal" />
      <el-table-column label="商品规格" align="center" prop="specs" />
      <el-table-column label="生产日期" align="center" prop="productionDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.productionDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="过期日期" align="center" prop="expireDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.expireDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="wdm_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="备注信息" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyi:goodsDetail:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyi:goodsDetail:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改商品订单详情信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="goodsDetailRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="详情号" prop="detailCode">
          <el-input v-model="form.detailCode" placeholder="请输入详情号" />
        </el-form-item>
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="商品ID" prop="goodsId">
          <el-input v-model="form.goodsId" placeholder="请输入商品ID" />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品主图" prop="mainImage">
          <image-upload v-model="form.mainImage" />
        </el-form-item>
        <el-form-item label="下单时单价" prop="unitPrice">
          <el-input v-model="form.unitPrice" placeholder="请输入下单时单价" />
        </el-form-item>
        <el-form-item label="购买数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入购买数量" />
        </el-form-item>
        <el-form-item label="子项金额" prop="subtotal">
          <el-input v-model="form.subtotal" placeholder="请输入子项金额" />
        </el-form-item>
        <el-form-item label="商品规格" prop="specs">
          <el-input v-model="form.specs" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="生产日期" prop="productionDate">
          <el-date-picker clearable v-model="form.productionDate" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择生产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="过期日期" prop="expireDate">
          <el-date-picker clearable v-model="form.expireDate" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择过期日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in wdm_status" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="删除" prop="delFlag">
          <el-radio-group v-model="form.delFlag">
            <el-radio v-for="dict in del_flag" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="GoodsDetail">
import {
  listGoodsDetail,
  getGoodsDetail,
  delGoodsDetail,
  addGoodsDetail,
  updateGoodsDetail,
} from "@/api/ruoyi/goodsDetail";
import { listGoods } from "@/api/ruoyi/goods";
import { listGuest } from "@/api/ruoyi/guest";

const { proxy } = getCurrentInstance();
const { wdm_status, del_flag } = proxy.useDict("wdm_status", "del_flag");

const goodsDetailList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    detailCode: null,
    orderId: null,
    goodsId: null,
    goodsName: null,
    status: null,
  },
  rules: {
    detailCode: [
      { required: true, message: "详情号不能为空", trigger: "blur" },
    ],
    orderId: [{ required: true, message: "订单ID不能为空", trigger: "blur" }],
    goodsId: [{ required: true, message: "商品ID不能为空", trigger: "blur" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询商品订单详情信息列表 */
function getList() {
  loading.value = true;
  listGoodsDetail(queryParams.value).then((response) => {
    // goodsDetailList.value = response.rows;
    // 过滤已删除数据（假设 delFlag 字段标识删除状态）
    goodsDetailList.value = response.rows.filter(
      (item) => item.delFlag !== "2"
    );
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    detailId: null,
    detailCode: null,
    orderId: null,
    goodsId: null,
    goodsName: null,
    mainImage: null,
    unitPrice: null,
    quantity: null,
    subtotal: null,
    specs: null,
    productionDate: null,
    expireDate: null,
    status: null,
    delFlag: null,
    version: null,
    createTime: null,
    updateTime: null,
    createBy: null,
    updateBy: null,
    remark: null,
  };
  proxy.resetForm("goodsDetailRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.detailId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加商品订单详情信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _detailId = row.detailId || ids.value;
  getGoodsDetail(_detailId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改商品订单详情信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["goodsDetailRef"].validate((valid) => {
    if (valid) {
      if (form.value.detailId != null) {
        updateGoodsDetail(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addGoodsDetail(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _detailIds = row.detailId || ids.value;
  proxy.$modal
    .confirm('是否确认删除商品订单详情信息编号为"' + _detailIds + '"的数据项？')
    .then(function () {
      return delGoodsDetail(_detailIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "ruoyi/goodsDetail/export",
    {
      ...queryParams.value,
    },
    `goodsDetail_${new Date().getTime()}.xlsx`
  );
}

// const goodsList = ref([]);
// const guestList = ref([]);

// // 查询顾客信息
// function getGoodsList() {
//   listGoods(loadAllParams).then((response) => {
//     goodsList.value = response.rows;
//   });
// }

// // // 查询商品信息
// function getGueatList() {
//   listGuest(loadAllParams).then((response) => {
//     guestList.value = response.rows;
//   });
// }

// getGueatList(); //  获取顾客信息

// getGoodsList(); //  获取商品信息

getList();
</script>
